package com.example.service;

import com.example.model.User;
import com.example.repository.UserRepository;

import java.util.List;

/**
 * 用户业务逻辑层
 */
public class UserService {
    private final UserRepository userRepository;
    
    public UserService() {
        this.userRepository = new UserRepository();
    }
    
    /**
     * 获取所有用户
     */
    public List<User> getAllUsers() {
        return userRepository.findAll();
    }
    
    /**
     * 根据ID获取用户
     */
    public User getUserById(int id) {
        return userRepository.findById(id);
    }
    
    /**
     * 根据邮箱获取用户
     */
    public User getUserByEmail(String email) {
        return userRepository.findByEmail(email);
    }
    
    /**
     * 创建新用户
     * @param name 用户名
     * @param email 邮箱
     * @return 创建结果
     */
    public UserCreationResult createUser(String name, String email) {
        // 输入验证
        if (name == null || name.trim().isEmpty()) {
            return new UserCreationResult(false, "用户名不能为空");
        }
        
        if (email == null || email.trim().isEmpty()) {
            return new UserCreationResult(false, "邮箱不能为空");
        }
        
        // 验证邮箱格式
        if (!isValidEmail(email)) {
            return new UserCreationResult(false, "邮箱格式不正确");
        }
        
        // 检查邮箱是否已存在
        if (userRepository.existsByEmail(email)) {
            return new UserCreationResult(false, "邮箱已存在");
        }
        
        // 创建用户
        boolean success = userRepository.save(name.trim(), email.trim());
        
        if (success) {
            return new UserCreationResult(true, "用户创建成功");
        } else {
            return new UserCreationResult(false, "用户创建失败");
        }
    }
    
    /**
     * 更新用户信息
     */
    public UserUpdateResult updateUser(User user) {
        if (user == null || user.getId() <= 0) {
            return new UserUpdateResult(false, "无效的用户信息");
        }
        
        // 输入验证
        if (user.getName() == null || user.getName().trim().isEmpty()) {
            return new UserUpdateResult(false, "用户名不能为空");
        }
        
        if (user.getEmail() == null || user.getEmail().trim().isEmpty()) {
            return new UserUpdateResult(false, "邮箱不能为空");
        }
        
        // 验证邮箱格式
        if (!isValidEmail(user.getEmail())) {
            return new UserUpdateResult(false, "邮箱格式不正确");
        }
        
        // 检查邮箱是否被其他用户占用
        User existingUser = userRepository.findByEmail(user.getEmail());
        if (existingUser != null && existingUser.getId() != user.getId()) {
            return new UserUpdateResult(false, "邮箱已被其他用户使用");
        }
        
        // 更新用户
        boolean success = userRepository.update(user);
        
        if (success) {
            return new UserUpdateResult(true, "用户更新成功");
        } else {
            return new UserUpdateResult(false, "用户更新失败");
        }
    }
    
    /**
     * 删除用户
     */
    public UserDeletionResult deleteUser(int id) {
        if (id <= 0) {
            return new UserDeletionResult(false, "无效的用户ID");
        }
        
        // 检查用户是否存在
        User user = userRepository.findById(id);
        if (user == null) {
            return new UserDeletionResult(false, "用户不存在");
        }
        
        // 删除用户
        boolean success = userRepository.deleteById(id);
        
        if (success) {
            return new UserDeletionResult(true, "用户删除成功");
        } else {
            return new UserDeletionResult(false, "用户删除失败");
        }
    }
    
    /**
     * 获取用户总数
     */
    public int getUserCount() {
        return userRepository.count();
    }
    
    /**
     * 验证邮箱格式
     */
    private boolean isValidEmail(String email) {
        if (email == null || email.trim().isEmpty()) {
            return false;
        }
        
        String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@" +
                           "(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
        
        return email.matches(emailRegex);
    }
    
    /**
     * 用户创建结果
     */
    public static class UserCreationResult {
        private final boolean success;
        private final String message;
        
        public UserCreationResult(boolean success, String message) {
            this.success = success;
            this.message = message;
        }
        
        public boolean isSuccess() {
            return success;
        }
        
        public String getMessage() {
            return message;
        }
    }
    
    /**
     * 用户更新结果
     */
    public static class UserUpdateResult {
        private final boolean success;
        private final String message;
        
        public UserUpdateResult(boolean success, String message) {
            this.success = success;
            this.message = message;
        }
        
        public boolean isSuccess() {
            return success;
        }
        
        public String getMessage() {
            return message;
        }
    }
    
    /**
     * 用户删除结果
     */
    public static class UserDeletionResult {
        private final boolean success;
        private final String message;
        
        public UserDeletionResult(boolean success, String message) {
            this.success = success;
            this.message = message;
        }
        
        public boolean isSuccess() {
            return success;
        }
        
        public String getMessage() {
            return message;
        }
    }
} 